package com.amumu.drama.common.modules.invitation.service.impl;

import cn.hutool.core.util.StrUtil;
import com.amumu.drama.common.base.api.CommonPage;
import com.amumu.drama.common.constant.IntegrationRewardStatusEnum;
import com.amumu.drama.common.constant.InvitationTypeEnum;
import com.amumu.drama.common.modules.invitation.dto.req.InvitationLogQuery;
import com.amumu.drama.common.modules.invitation.dto.resp.MemberInvitationLogListResp;
import com.amumu.drama.common.modules.invitation.mapping.MemberInvitationLogListRespMapping;
import com.amumu.drama.common.modules.invitation.service.InvitationLogService;
import com.amumu.drama.mapper.InvitationLogMapper;
import com.amumu.drama.model.InvitationLog;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sunzhuang
 * @date 2024/9/13 10:45
 * @slogan 天下风云出我辈, 一入代码岁月催
 * @desc todo
 */
@Service
public class InvitationLogServiceImpl extends ServiceImpl<InvitationLogMapper, InvitationLog> implements InvitationLogService {

    @Override
    public CommonPage<MemberInvitationLogListResp> queryInvitationLog(InvitationLogQuery request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<InvitationLog> invitationLogList = this.lambdaQuery().eq(InvitationLog::getInvitationCode, request.getInvitationCode())
                .eq(InvitationLog::getType, InvitationTypeEnum.MEMBER.getCode()).list();
        return CommonPage.restPageAndConvert(invitationLogList,MemberInvitationLogListRespMapping.INSTANCE::convertTo);
    }

    @Override
    public Long selectInvitationCountByMemberId(String invitationType, String invitationCode) {
        if (StrUtil.isBlank(invitationCode)) {
            return 0L;
        }
        return this.lambdaQuery()
                .eq(InvitationLog::getInvitationCode, invitationCode)
                .eq(InvitationLog::getType, invitationType)
                .eq(InvitationLog::getDeleteStatus, 0)
                .eq(InvitationLog::getIntegrationStatus, IntegrationRewardStatusEnum.SUCCESS.getCode())
                .count();
    }
}
